<?php
namespace Wap\Controller;
class UserController extends CommonController{
	// 过滤不需要登录动作
	public $filter_action = array(
		'register', 
		'forget_password', 
		'valid_field', 
		'get_smscode', 
		'lawyer_register'
	);

	public function _initialize(){
		parent::_initialize();
		if(!in_array(ACTION_NAME, $this->filter_action)){
			$this->user_id();
			if($this->user['is_lawyer']){
				redirect(U('LawyerCenter/perfect_data'));
			}
		}
	}
	//用户注册
	public function register(){
		$this->add_step('用户注册');
		if(IS_POST){
			$mobile     = $this->_post('mobile', '请输入手机号！');
			$smscode    = $this->_post('smscode', '短信验证码不能为空！');
			$password   = $this->_post('password', '请输入密码！');
			$repassword = $this->_post('repassword', '请再次输入密码！');
			$email      = $this->_post('email', 'E-mail不能为空！');
			if($password != $repassword){
			    $this->error('两次密码不一致！');
			}
			if(D('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}
			$password_md5 = md5($password);
			$user_info = D('User')->register($mobile, $email, $password_md5, 0, 'wap');
			if($user_info){
				D('User')->save_session($user_info);
				$this->success('注册成功！', U('User/index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}

	//律师注册
	public function lawyer_register(){
		$this->add_step('律师注册');
		$this->is_show_footer = false;
		if(IS_POST){
			$mobile     = $this->_post('mobile', '请输入手机号！');
			$smscode    = $this->_post('smscode', '短信验证码不能为空！');
			$password   = $this->_post('password', '请输入密码！');
			$repassword = $this->_post('repassword', '请再次输入密码！');
			$email      = $this->_post('email', 'E-mail不能为空！');
			if($password != $repassword){
			    $this->success('两次密码不一致！');
			}
			if(D('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}
			$password_md5 = md5($password);
			$user_info = D('User')->register($mobile, $email, $password_md5, 1, 'wap');
			if($user_info){
				D('User')->save_session($user_info);
				$this->success('注册成功！', U('LawyerCenter/perfect_data'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}
	//个人信息
	public function info(){
		$this->add_step('个人信息');
		if(IS_POST){
			$realname = $this->_post('realname');
			$nickname = $this->_post('nickname');
			$sex      = $this->_post('sex');
			if(D('User')->edit_info($this->user_id, $realname, $nickname, $sex)){
				$this->success('修改成功！', URL('User/info'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->assign('info', $this->user);
		$this->display();
	}
	//更换手机号
	public function change_mobile(){
		$this->add_step('更换手机号');
		if(IS_POST){
			$mobile   = $this->_post('mobile', '手机号不能为空！');
			$smscode  = $this->_post('smscode', '短信验证码不能为空！');
			$password = $this->_post('password', '密码不能为空！');
			$password = md5($password);
			if(!D('User')->check_password($this->user_id, $password)){
				$this->error(D('User')->tips_info);
			}

			if(D('User')->change_mobile($this->user_id, $mobile)){
				//更新一下用户信息
				D('User')->get_user_info();
				$this->success('修改成功！', URL('User/index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}

		$this->display();
	}
	//更换E-mail
	public function change_email(){
		$this->add_step('更换E-mail');
		if(!$this->set_password){
			$this->error('请先设置密码！');
		}
		if(IS_POST){
			$email    = $this->_post('email', 'E-mail不能为空！');
			$password = $this->_post('password', '密码不能为空！');
			$password = md5($password);
			if(!D('User')->check_password($this->user_id, $password)){
				$this->error(D('User')->tips_info);
			}

			if(D('User')->change_email($this->user_id, $email)){
				//更新一下用户信息
				D('User')->get_user_info();
				$this->success('修改成功！',  URL('User/info'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}

		$this->display();
	}
	//修改密码
	public function change_password(){
		$this->add_step('修改密码');
		if(IS_POST){
			$old_password = $this->set_password ? $this->_post('old_password', '旧密码不能为空！') : '';
			$new_password = $this->_post('password', '新密码不能为空！');

			$old_password = md5($old_password);
			$new_password = md5($new_password);
			if(D('User')->change_password($this->user_id, $old_password, $new_password)){
				$this->success('修改成功！', U('User/index'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}

	//找回密码
	public function forget_password(){
		$this->add_step('找回密码');
		if(IS_POST){
			$mobile     = $this->_post('mobile', '请输入手机号！');
			$smscode    = $this->_post('smscode', '验证码不能为空！');
			$password   = $this->_post('password', '请输入密码！');
			$repassword = $this->_post('repassword', '请再次输入密码！');
			if($password != $repassword){
			    $this->success('两次密码不一致！');
			}
			if(D('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}

			$password = md5($password);
			if(D('User')->forget_password($mobile, $password)){
				$this->success('找回密码成功！', URL('Login/index', '', 'login'));
			}else{
				$this->error(D('User')->tips_info);
			}
		}
		$this->display();
	}

	//退出登录
	public function logout(){
		session(null);
		redirect(URL('Login/index', '', 'login'));
	}
}